//
//  SininWorkHistoryViewController.swift
//  DSHCMerchant
//
//  Created by 罗德良 on 2018/5/5.
//  Copyright © 2018年 袋鼠好车. All rights reserved.
//

import UIKit
import Alamofire
class SininWorkHistoryViewController: BaseViewController {
    @IBOutlet var calendar: FDCalendar!
    @IBOutlet var timeLabel: UILabel!
    @IBOutlet var checkWorkTableView: UITableView!
    var workTimes:[WorkTimeModel]?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        requestGetWorkTime()
    requestGetDayWorkTimeList(dateStr:NSDate.currentDateDayString())
        createCalendar()
        configView()
        
    }
    
    func configView() -> Void {
        checkWorkTableView.delegate = self
        checkWorkTableView.dataSource = self
        checkWorkTableView.separatorStyle = .none
        checkWorkTableView.showsVerticalScrollIndicator = false
        checkWorkTableView.backgroundColor = BaseColor.BackGroundColor
        checkWorkTableView.register(UINib(nibName: "WorkTimeTableViewCell", bundle: nil), forCellReuseIdentifier: "worktimecell")
        //        couponTableview.cr.addHeadRefresh(animator: SlackLoadingAnimator() as CRRefreshProtocol) { [weak self] in
        //            self?.requstStoreConfigList()
        //        }
    }
    
    func createCalendar() -> Void {
        calendar = FDCalendar.init(currentDate: NSDate() as Date?)
        calendar.delegate = self as FDCalendarSendTimeDelegate
        self.view.addSubview(calendar)
    }
    
    
    
    
    //获取上班时间
    func requestGetWorkTime() -> Void {
        Alamofire.request(getWorkTimeUrl, method:.post, parameters:["sid":UserModel.shareUser.data?.storeId ?? String(),"uid":UserModel.shareUser.data?.uid ?? String()])
            .responseString { response in
                
                switch response.result.isSuccess {
                case true:
                    
                    var basemodel = BaseResponseOfModel<BaseArrayData<WorkTimeModel>>()
                    basemodel = BaseResponseOfModel.deserialize(from: response.value)!
                    
                    switch basemodel.status! {
                    case true:
                        
                        if(basemodel.data?.data?.count)! > 0 {
                            let worktime = basemodel.data?.data![0]
                            let timestr = (worktime?.go_work_time)! + "-" + (worktime?.closing_work_time)!
                            self.timeLabel.text = timestr
                        }
                        
                        
                        print("获取成功")
                    case false:
                         MBProgressHUD.alertShow(in: self.view, message:basemodel.msg, hidenAfter: 0.8, mode: MBProgressHUDMode.text)
                    }
                    
                case false:
                    print(response.result.error!)
                }
        }
    }
    
    //获取员工自身打卡列表
    func requestGetDayWorkTimeList(dateStr:String) -> Void {
        Alamofire.request(getEmployeeWorkTimeListUrl, method:.post, parameters:["sid":UserModel.shareUser.data?.storeId ?? String(),"uid":UserModel.shareUser.data?.uid ?? String(),"sign_date":dateStr])
            .responseString { response in
                
                switch response.result.isSuccess {
                case true:
                    
                    var basemodel = BaseResponseOfModel<BaseArrayData<WorkTimeModel>>()
                    basemodel = BaseResponseOfModel.deserialize(from: response.value)!
                    switch basemodel.status! {
                    case true:
                        self.workTimes = basemodel.data?.data
                        self.checkWorkTableView.reloadData()
                        print("获取成功")
                    case false:
                         MBProgressHUD.alertShow(in: self.view, message:basemodel.msg, hidenAfter: 0.8, mode: MBProgressHUDMode.text)
                    }
                    
                case false:
                    print(response.result.error!)
                }
        }
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}

extension SininWorkHistoryViewController:FDCalendarSendTimeDelegate,UITableViewDelegate,UITableViewDataSource {
    
    func sendTime(_ date: Date!) {
        
        requestGetDayWorkTimeList(dateStr:NSDate.transformString(withFormat: "yyyy-MM-dd", get: date as Date?))
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.workTimes?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "worktimecell", for: indexPath) as! WorkTimeTableViewCell
        cell.selectionStyle = .none
        let model = self.workTimes![indexPath.item]
        cell.nameLabel.text = model.name
        cell.goworkTimeLabel.text = model.go_work_time
        cell.downWorkTimeLabel.text = model.closing_work_time
        if model.is_late == "0" {
            cell.goworkTimeLabel.textColor = UIColor.black
        }else {
            cell.goworkTimeLabel.textColor = UIColor.red
        }
        if model.is_early == "0" {
            cell.downWorkTimeLabel.textColor = UIColor.black
        }else {
            cell.downWorkTimeLabel.textColor = UIColor.red
        }
        
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return ScaleHeight(40)
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
}
